﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability_Management.Domain;
using Traceability_Management.ErrorCode;
using Traceability_Management.Infrastructure.Interfaces;
using Traceability_Management.Read.Api.Application.Command.FamingWay;

namespace Traceability_Management.Read.Api.Application.CommandHandler.FamingWay
{
    public class FirstFarmingWayCommandHandler : IRequestHandler<FirstFarmingWayCommand, ApiResult<Farmings>>
    {
        IBaseReponsitory<Farmings> Farmings;

        public FirstFarmingWayCommandHandler(IBaseReponsitory<Farmings> Farmings)
        {
            this.Farmings = Farmings;
        }

        public async Task<ApiResult<Farmings>> Handle(FirstFarmingWayCommand request, CancellationToken cancellationToken)
        {
           var Bachlist = await Farmings.GetQuery().FirstOrDefaultAsync(c => c.Rabit_Batch == request.Batch);
            if (Bachlist != null)
            {
                return new ApiResult<Farmings>
                {
                    Code = ApiEnum.Success,
                    Message = "加载成功了",
                    Data = Bachlist
                };
            }
            else 
            {
                return new ApiResult<Farmings>
                {
                    Code = ApiEnum.Success,
                    Message = "请设置信息",
                };
            }
        }
    }
}
